<?php
error_reporting(E_ALL);
require_once  dirname(__FILE__).'/../code/CV.php';
CV::init();

$pdo = new CV_PDO('mysql:dbname=kalender;host=127.0.0.1','root');
$pdo->query("SET SESSION sql_mode=STRICT_TRANS_TABLES");
$ds = CV_Datastore::create( $pdo );
$ds->kickstart( 'datastore_test' );

if( isset($_GET['success']) ){
	echo 'Form submitted and saved. <br /><br /><a href="?">restart</a>';
} elseif( isset($_GET['edit']) || isset($_GET['new']) || isset($_GET['submit']) ) {
	$class = $ds->object_schema['person'];
	$class['Deleted']->is_protected = true;
	if( isset($_GET['edit']) ){
		$record = $ds->find_one( 'person', 'id = '.$_GET['edit'] );
		$form = $ds->create_builder( $record );
	} else {
		$form = $ds->create_builder( 'person' );
	}
	if( !$form->is_submitted() || !$form->is_valid() ){
		echo $form->render();
		echo '<a href="?">cancel</a>';
	} else {
		$ds->save( $form->get_result() );
		header( 'Location: ?success' );
	}
} else {
	$persons = $ds->find( 'person' );
?>
<a href="?new">neuer Eintrag</a>
<table>
<?foreach( $persons as $person ):?>
	<tr>
<?foreach( $person as $field ):?>
		<td>
			<?=$field?>
		</td>
<?endforeach?>
		<td>
			<a href="?edit=<?=$person['id']?>">edit</a>
		</td>
	</tr>
<?endforeach?>
</table>
<?
}
class person extends ArrayObject{}